package knowlwdge_graph.knowledge_etl.relation_etl;

import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientDynaElementIterable;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
import common.dao.orient.OrientGraphDao;
import javafx.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by julianzliu on 5/5/2017.
 */
public class RelationExtractor {

    static final Logger logger = LoggerFactory.getLogger( RelationExtractor.class );


    private OrientGraphDao graphDao;
    private OrientGraph orientGraph;

    public RelationExtractor(){
        this.graphDao = new OrientGraphDao();
        this.orientGraph = this.graphDao.getGraph();
    }


    public void insertRelationGloss(String yago_relation, String gloss){
        /*
        1）检索 relation 对应的 rid
        2) 创建 gloss 节点并返回 rid
        3）创建 边 <hasGloss> 进行关联
         */

        String from  = getRidByYagoId(yago_relation);
        String to = createGlossVertex(gloss);
        String edgeid = createGlossEdge(from, to);

    }


    private String getRidByYagoId(String yago_id){
        String sql = String.format("select * from V where yago_id='%s';", yago_id);
        logger.info("[getRidByYagoId] sql = {}", sql);

        Iterable<Vertex> vertices = this.orientGraph.command(
                new OCommandSQL(sql )
        ).execute();

        String rid = null;

        for(Vertex row: vertices){
            Vertex vertex = row.getProperty("@rid");
            rid = (String)vertex.getId().toString();
        }

        logger.info("[getRidByYagoId] Rid = {}", rid);
        System.out.println("id = " + rid);
        return rid;
    }

    private String createGlossVertex(String gloss){
        String sql = String.format("create vertex YAGO_RELATION_GLOSS_CLASS set yago_label=\"%s\";", gloss);
        logger.info("[createGlossVertex] sql = {}", sql);

        OrientVertex v = this.orientGraph.command(
                new OCommandSQL(sql )
        ).execute();

        String rid = null;

        Vertex vertex = v.getProperty("@rid");
        rid = (String)vertex.getId().toString();


        logger.info("[createGlossVertex] Rid = {}", rid);
        System.out.println("id = " + rid);
        return rid;
    }


    private String createGlossEdge(String fromRid, String toRid){
        String sql = String.format("create edge YAGO_RELATION_GLOSS_EDGE from %s to %s", fromRid, toRid);
        logger.info("[createGlossEdge] sql = {}", sql);

        OrientDynaElementIterable v = this.orientGraph.command(
                new OCommandSQL(sql )
        ).execute();

        /*
        String rid = null;

        Vertex vertex = v.getProperty("@rid");
        rid = (String)vertex.getId().toString();


        logger.info("[createGlossEdge] Rid = {}", rid);
        System.out.println("id = " + rid);
        return rid;
        */

        return null;
    }


    /*************************************************************
     *
     * @param args
     */
    public static void main(String[] args){
       RelationExtractor extractor = new RelationExtractor();
       //String from  = extractor.getRidByYagoId("<isMarriedTo>");
       //String to = extractor.createGlossVertex("husband , wife ");
       //String edgeid = extractor.createGlossEdge(from, to);

        extractor.insertRelationGloss("<isMarriedTo>", "husband , wife ");

    }



}
